package com.fish.passport.processor;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fish.order.po.Order;
import com.fish.passport.dao.PassCodeDAO;
import com.fish.passport.dao.PassPortCodeDAO;
import com.fish.passport.po.PassCode;
import com.fish.passport.po.PassPortCode;
import com.fish.service.BusinessLogicService;
import com.fish.service.order.OrderService;
import com.fish.system.dao.ComLogDAO;
import com.fish.system.po.ComLog;
import com.fish.vo.Passport;
import com.fish.vo.PassportConstant;

/**
 * 回收回调实现
 * 
 * @author chenlinjun
 * @date:2010-9-25
 */
public class UsedCodeProcessor {
	private static final Log log = LogFactory.getLog(UsedCodeProcessor.class);
	private PassCodeDAO passCodeDAO;
	private PassPortCodeDAO passPortCodeDAO;
	private ComLogDAO comLogDAO ;
	private BusinessLogicService businessLogicService;
	private OrderService orderService;
	/**
	 * 
	 * 回收回调参数列表<br>
	 * 申请流水号（String） 状态（String）
	 */
	public String update(Passport data) {
		PassCode passCode = passCodeDAO.getCodeBySerialNo(data.getSerialno());
		PassPortCode passPortCode = new PassPortCode();
		passPortCode.setCodeId(passCode.getCodeId());
		passPortCode.setTargetId(data.getPortId());
		passPortCode.setStatus(PassportConstant.PASSCODE_USE_STATUS.USED.name());
		if(data.getUsedDate()!=null){
			passPortCode.setUsedTime(new Timestamp(data.getUsedDate().getTime()));
		}else{
			passPortCode.setUsedTime(new Timestamp(new Date().getTime()));
		}
		// 更新通关点信息
		passPortCodeDAO.updatePassPortCode(passPortCode);
		//使用码成功，更新码的修改时间
		PassCode temp=new PassCode();
		temp.setUpdateTime(new Date());
		temp.setCodeId(passCode.getCodeId());
		passCodeDAO.updatePassCode(temp);
		
		String result = addOrderPerform(data, passCode);
		return result;
	}

	private String addOrderPerform(Passport data, PassCode passCode) {
		String flag = PassportConstant.PASSCODE_APPLY_STATUS.SUCCESS.name();
		// 成人数
		Long adultQuantity = Long.valueOf(data.getAdult());
		// 儿童数
		Long childQuantity = Long.valueOf(data.getChild());
		log.info("adultQuantity Total:" + adultQuantity);
		log.info("childQuantity Total:" + childQuantity);
		StringBuilder buf=new StringBuilder();
		buf.append("设备号:"+data.getDeviceId());
		buf.append(",adultQuantity:"+adultQuantity);
		buf.append(",childQuantity:"+childQuantity);
		buf.append(",TargetId:"+data.getOutPortId());
		buf.append(",OrderId:"+passCode.getOrderId());
		
		Order order=orderService.queryOrderByPrimaryKey(passCode.getOrderId());
		this.businessLogicService.orderFerformedLogic(order);
		this.passLogs(passCode.getOrderId(), buf.toString(), passCode.getCodeId());
		return flag;
	}
	/**
	 * 通关日志
	 * @param orderId
	 * @param content
	 * @param codeId
	 */
	private void passLogs(Long orderId,String content,String codeId){
		ComLog log = new ComLog();
		log.setObjectType("PASS_CODE");
		log.setParentId(orderId.toString());
		log.setObjectId(codeId);
		log.setOperatorName("SYSTEM");
		log.setLogType("SYSTEM");
		log.setLogName("设备刷码通关");
		log.setContent(content);
		//comLogDAO.insert(log);
	}

	public void callback() {
		// 通关系统和第三系统通信返回数据
		// Map<String, String> data = new HashMap<String, String>();
		// data.put("passport.portId", this.data.getOutPortId());
		// data.put("passport.serialno", this.data.getSerialno());
		// data.put("passport.child", this.data.getChild());
		// data.put("passport.adult", this.data.getAdult());
		// log.info("回收回调处理参数："+data.values());
		// SendRequest sendRequest = new SendRequest();
		// String result = sendRequest.send(new
		// UsedCodeRequest(data,this.data.getUrl()));
	}

	public void setPassCodeDAO(PassCodeDAO passCodeDAO) {
		this.passCodeDAO = passCodeDAO;
	}

	public void setPassPortCodeDAO(PassPortCodeDAO passPortCodeDAO) {
		this.passPortCodeDAO = passPortCodeDAO;
	}
	public void setComLogDAO(ComLogDAO comLogDAO) {
		this.comLogDAO = comLogDAO;
	}

	public void setBusinessLogicService(BusinessLogicService businessLogicService) {
		this.businessLogicService = businessLogicService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	
}
